package com.xinxin.blockchain.dao.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinxin.blockchain.dao.ISystemConfigDao;
import com.xinxin.blockchain.mapper.SystemConfigMapper;
import com.xinxin.blockchain.model.SystemConfig;
import org.apache.ibatis.exceptions.TooManyResultsException;

import java.util.List;

/**
 * <p>
 * 系统参数配置表 服务实现类
 * </p>
 *
 * @author ys
 * @since 2021-06-08
 */
public class SystemConfigDao extends ServiceImpl<SystemConfigMapper, SystemConfig> implements ISystemConfigDao {

    @Override
    public SystemConfig getConfig(Integer type, String key) {
        List<SystemConfig> configList = listConfig(type, key);
        if (configList.size() > 1) {
            throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + configList.size());
        }

        return configList.isEmpty() ? null : configList.get(0);
    }

    @Override
    public List<SystemConfig> listConfig(Integer type, String key) {
        Wrapper<SystemConfig> queryWrapper = new QueryWrapper<SystemConfig>().lambda()
                .eq(SystemConfig::getDataStatus, true)
                .eq(SystemConfig::getType, type)
                .eq(SystemConfig::getKey, key);
        return baseMapper.selectList(queryWrapper);
    }
}
